package com.wutong.authority_server.security;


import com.alibaba.fastjson.JSONObject;
import com.wutong.authority_server.entity.ResultCode;
import com.wutong.common_server.result.Response;
import com.wutong.common_server.utils.ResponseUtil;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
//匿名用户访问无权限资源时的异常
public class UnauthEntryPoint implements AuthenticationEntryPoint {
    @Override
    public void commence(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AuthenticationException e) throws IOException, ServletException {
        //用户未登录
        Response failure = ResponseUtil.failure(ResultCode.USER_NOT_LOGIN.getMessage(),ResultCode.USER_NOT_LOGIN.getCode());
        httpServletResponse.setCharacterEncoding("utf-8");
        httpServletResponse.setContentType("text/json;charset=utf-8");
        httpServletResponse.getWriter().print(JSONObject.toJSONString(failure));

    }
}
